package com.gin.no_gay_alliance.fragment.history;

import android.os.Bundle;
import android.util.Log;

import com.gin.jackson.utils.JacksonUtils;
import com.gin.jackson.utils.ObjectUtils;
import com.gin.no_gay_alliance.adpter.history.ReplyHistoryAdapter;
import com.gin.no_gay_alliance.database.entity.ReplyHistory;
import com.gin.no_gay_alliance.database.manager.ReplyHistoryManager;
import com.gin.no_gay_alliance.database.where.ReplyHistoryWhere;
import com.gin.no_gay_alliance.databinding.FragmentReplyHistoryPageBinding;
import com.gin.no_gay_alliance.enums.MessageWhat;
import com.gin.no_gay_alliance.exception.RequireParamException;
import com.gin.no_gay_alliance.fragment.base.BaseBindingFragment;
import com.gin.no_gay_alliance.viewmodel.PageData;

/**
 * 回复历史搜索的一页
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/9/18 17:00
 **/
public class ReplyHistorySearchPageFragment extends BaseBindingFragment<FragmentReplyHistoryPageBinding> {
    private static final String ARG_PAGE = "page";
    private ReplyHistoryWhere where;
    private int page;

    /**
     * onCreateView 的后续操作
     */
    @Override
    protected void onCreateView() {
        JacksonUtils.printPretty(where);
        Log.d(TAG, "onCreateView: " + page);

        PageData<ReplyHistory> pageData = ReplyHistoryManager.page(where, page);

        b.recyclerView.setAdapter(new ReplyHistoryAdapter(requireContext(), pageData.data));

        if (ObjectUtils.isEmpty(pageData.data)) {
            toast("没有找到符合的结果");
        }

        // 查询上报给父级
        getParentHandler().sendMessage(MessageWhat.ON_SUCCESS_REPLY_HISTORY_PAGE, pageData);
    }

    /**
     * 获取和处理参数
     *
     * @param arguments 参数
     */
    @Override
    protected void handleArguments(Bundle arguments) {
        String key = ReplyHistoryWhere.class.getName();
        if (arguments != null && arguments.getParcelable(key) != null) {
            this.where = arguments.getParcelable(key);
            this.page = arguments.getInt(ARG_PAGE, 1);
        } else {
            throw new RequireParamException(key);
        }
    }

    public static ReplyHistorySearchPageFragment newInstance(ReplyHistoryWhere where, int page) {

        ReplyHistorySearchPageFragment fragment = new ReplyHistorySearchPageFragment();
        Bundle args = new Bundle();
        args.putParcelable(ReplyHistoryWhere.class.getName(), where);
        args.putInt(ARG_PAGE, page);
        fragment.setArguments(args);
        return fragment;
    }
}
